package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.platform.entity.User;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 平台基础用户
 */
@Repository
public class UserDao extends BaseDao<User> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存或者更新
     */
    public void save(User user) {
        if (user.getId() != null && user.getId() > 0) {
            this.update(user);
        } else {
            user.setId(this.add(user));
        }
    }

    //详情
    public User get(Integer id) {
        if (id == null || StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select bu.*,(select bo.title  from bt_organ bo  where bo.id = bu.orgid) orgname from bt_user bu  where 1=1");
        sbStr.append(" and bu.id = ? ");
        paramList.add(id);
        sbStr.append(" ORDER BY bu.sorts DESC ");
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, User.class);
    }

    //列表查询
    public List<User> findList(Map<String, Object> paraMap, SplitPageBean splitPage) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select bu.*,(select bo.title  from bt_organ bo  where bo.id = bu.orgid) orgname from bt_user bu  where 1=1");
        if (StringEx.isNotNull(paraMap.get("loginname"))) {
            sbStr.append(" and loginname like ? ");
            paramList.add(paraMap.get("loginname"));
        }
        if (StringEx.isNotNull(paraMap.get("password"))) {
            sbStr.append(" and password = ? ");
            paramList.add(paraMap.get("password"));
        }

        if (StringEx.isNotNull(paraMap.get("name"))) {
            sbStr.append(" and name like ? ");
            paramList.add("%" + paraMap.get("name") + "%");
        }

        if (StringEx.isNotNull(paraMap.get("states"))) {
            sbStr.append(" and states = ? ");
            paramList.add(paraMap.get("states"));
        }
        if (StringEx.isNotNull(paraMap.get("orgid"))) {
            sbStr.append(" and orgid = ? ");
            paramList.add(paraMap.get("orgid"));
        }
        if (StringEx.isNotNull(paraMap.get("orgidIn"))) {
            sbStr.append(" and orgid in ( ");
            Integer[] orgidArray = (Integer[]) paraMap.get("orgidIn");
            if (orgidArray.length > 0) {
                for (Integer orgid : orgidArray) {
                    sbStr.append(orgid + " ,");
//                    paramList.add(orgid);
                }
                sbStr.append(" 0 ");
            }
            sbStr.append(" ) ");
        }
        sbStr.append(" ORDER BY bu.sorts DESC ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, User.class, splitPage);
    }


    /**
     * 删除
     */
    public void del(int id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_user  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public List<User> findOrgUserList(Map<String, Object> paraMap, SplitPageBean splitPage) {

        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_user where code is not null");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, User.class, splitPage);
    }
}
